<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>用户管理</h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="姓名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="姓名" align="center"></el-table-column>
                <el-table-column prop="roleNames" label="角色" align="center"></el-table-column>
                <el-table-column label="性别" align="center">
                    <template slot-scope="scope">
                    <span v-if="scope.row.gender == '1'">男</span>
                    <span v-if="scope.row.gender == '4'">女</span>
                    </template>

                    <!--<template slot-scope="scope">
                        <span>{{ scope.row.gender == '1' ? '男' : '女'}}</span>
                    </template>-->
                </el-table-column>
                <el-table-column prop="birthday" label="出生日期" align="center" :formatter="formatDate" ></el-table-column>
                <el-table-column prop="telephone" label="电话" align="center"></el-table-column>
                <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="53px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="username">
                                    <el-input v-model="formData.username" placeholder="请输入姓名"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="角色" prop="roleId">
<!--
                                    <el-input v-model="formData.roles" placeholder="角色"/>
-->
                                    <el-select v-model="formData.roleId">
                                        <el-option :label="role.name" :value="role.id" v-for="role in roles">{{role.name}}</el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="性别" prop="gender">
                                    <el-select v-model="formData.gender">
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="4"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="生日" prop="birthday">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="电话" prop="telephone">
                                    <el-input v-model="formData.telephone" placeholder="请输入电话" onblur="function f() {
                                      fun();
                                    }"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="邮箱" prop="email">
                                    <el-input v-model="formData.email" placeholder="请输入邮箱"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="53px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="姓名" prop="username">
                                    <el-input v-model="formData.username"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">

                                <el-form-item label="性别" prop="gender">
                                    <el-select v-model="formData.gender" >
                                        <el-option label="男" value="1"></el-option>
                                        <el-option label="女" value="4"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="生日" prop="birthday">
                                    <el-date-picker
                                            v-model="formData.birthday"
                                            type="date"
                                            placeholder="选择日期">
                                    </el-date-picker>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="电话" prop="telephone">
                                    <el-input v-model="formData.telephone"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="邮箱" prop="email">
                                    <el-input v-model="formData.email"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            roles:[],
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                username: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                telephone: [
                    {
                        validator: function (rule, value, callback) {
                            var MobileRegex = /^[1][3,4,5,7,8][0-9]{9}$/;
                            if (!MobileRegex.test(value)) {
                                callback(new Error('手机号码格式不正确！'))  // 这里错误的信息只要调用callback回调函数，然后在函数里newerror填写错误信息即可
                            } else {
                                callback();  // 一定要有，这是表单校验成功后的回调，会返回一个boolean值，即true
                            }
                        }, trigger: 'blur'
                    }
                ],
                email: [
                    {
                        validator: function (rule, value, callback) {
                            var email = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
                            if (!email.test(value)) {
                                callback(new Error('邮箱格式不正确！'))  // 这里错误的信息只要调用callback回调函数，然后在函数里newerror填写错误信息即可
                            } else {
                                callback();  // 一定要有，这是表单校验成功后的回调，会返回一个boolean值，即true
                            }
                        }, trigger: 'blur'
                    }
                ],
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();//vue对象初始化后，调用分页查询方法完成分页查询
        },
        methods: {
            formatDate(row, column) {
                // 获取单元格数据
                let data = row[column.property]
                if (data === '') {
                    return ''
                } else {
                    let dt = new Date(data);
                    return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate();
                }
            },
            //编辑
            handleEdit() {
                //表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid) {
                        //表单校验通过
                        axios.post("/user/edit.do", this.formData).then((res) => {
                            if (res.data.flag) {
                            //修改成功
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                        } else {
                            this.$message.error(res.data.message);
                        }
                    }).finally(() => {
                            //不管成功失败都分页查询
                            this.findPage();
                        //隐藏编辑窗口
                        this.dialogFormVisible4Edit = false;
                    });
                    } else {
                        //表单校验失败
                        this.$message.error("表单校验失败");
                return false;
            }
            });
            },
            //添加
            handleAdd() {
                //表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    //alert(valid);
                    if (valid) {
                        //校验通过发送请求
                        console.log(this.formData);
                        axios.post("/user/add.do", this.formData).then((res) => {
                            //关闭新增窗口
                            this.dialogFormVisible = false;
                            if (res.data.flag) {
                                //弹出成功提示信息
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                                //新增后调用分页
                                this.findPage();
                            } else {
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        });
                    } else {
                        //校验不通过，弹出提示信息
                        this.$message.error("数据校验失败，请检查输入信息是否有误");
                return false;
            }
            });
            },
            //分页查询
            findPage() {
                //发送ajax请求，提交分页相关请求参数
                var param = {
                    //页码
                    currentPage: this.pagination.currentPage,
                    //每页记录数
                    pageSize: this.pagination.pageSize,
                    //查询条件
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage.do", param).then((res) => {
                    //解析响应数据，为模型数据赋值
                    //总记录数
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;

                    //每页数据集合
                    /*for (var value of rows) {
                        for (var object of value) {
                                var name = object.getName();
                        }
                    }*/
                });
            },
            // 重置表单
            resetForm() {
            },
            // 弹出添加窗口
            handleCreate() {
                //弹出新增窗口
                this.dialogFormVisible = true;

                //重置数据
                this.formData = {};
                axios.get("/role/findRoleNams.do").then((res) => {
                    if(res.data.flag){
                        this.roles = res.data.data;
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //弹出编辑窗口，回显数据
                this.dialogFormVisible4Edit = true;

                axios.get("/user/findById.do?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                    //有数据回显
                    this.formData = res.data.data;
                } else {
                    //没数据，弹出提示
                    this.$message.error(res.data.message);
                }
            });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                //设置最新页码
                this.pagination.currentPage = currentPage;

                //点击分页后重新调用 分页查询
                this.findPage();
            },
            // 删除
            handleDelete(row) {
                //alert(row.id);
                this.$confirm("您确定删除吗？", "提示", {
                    type: "waring"
                }).then(() => {
                    //alert("确定了");
                    axios.get("/user/delete.do?id=" + row.id).then((res) => {
                    if (res.data.flag)
                {
                    //删除成功
                    this.$message({
                        type: 'success',
                        message: res.data.message
                    });

                    //重新分页查询
                    this.findPage();
                }else {
                    //删除失败
                    this.$message.error(res.data.message);
                }
            }).catch((r) => {
                    this.showMessage(r);
            });

            }).catch(() => {
                    //alert("取消了");
                    this.$message({
                    type: 'info',
                    message: '操作已取消'
                });
            });
            },
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }
    })
</script>
</html>